<template>
    <div class="loginBox">
        <div class="logopic">
            <img src="../assets/logo.png" alt="" >
        </div>
        <div class="formTitle">用户登入</div>
        <div>
            <el-form ref="form" :model="form" label-width="80px" :rules='rules'>
                <el-form-item  prop='username' label='用户名' >
                   <el-input  v-model="form.username"  placeholder="输入用户名" suffix-icon="el-icon-user-solid" ></el-input>
                </el-form-item>
                <el-form-item  prop='password' label='密    码'>
                    <el-input type='password' v-model="form.password" placeholder="输入密码"  suffix-icon="el-icon-lock" ></el-input>
                 </el-form-item>
                 <el-form-item>
                   <el-button type="primary" @click="login" >登录</el-button>
                    <el-button type="info" @click="resetLoginForm">重置</el-button>
                 </el-form-item> 
            </el-form>
        </div>
    </div>
</template>
<script>

export default {
    data(){  
        return {
            form:{
                username:'admin',
                password:'123456'
            },
            //校验规则
            rules: {
               username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
                password: [
                    {  required: true, message: '请输入密码', trigger: 'blur' },
                    { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
                ],
           }
        }
    },
    methods:{
        login () {
          this.$refs.form.validate(async (valid) => { 
        if (!valid) return
        // 发请求
        const { data: res } = await this.$http.post('login', this.form)
        if (res.meta.status !== 200) {
          return this.$message({
            message: '登录失败',
            type: 'error'
          })
        }
        this.$message.success('登录成功')
        // 1  把token保存起来
        window.sessionStorage.setItem('token', res.data.token)
        // 2 跳转主页
        this.$router.push('/home')
      })
    },
        resetLoginForm(){
            this.$refs.form.resetFields();
        }
    }
}
</script>
<style lang="less" scoped>
    .formTitle{
        position: absolute;
        top: 40px;
        left: 42%;
        color: #ff0f02;
        width: 100px;
        line-height: 50px;
        font-size: 25px;;
    }
    .loginBox{
        width: 500px;
        height: 300px;
        position: absolute;
        top: 50%;
        left: 70%;
        transform: translate(-50%,-50%);
        background-color:#fff;
        padding: 0 10px;
        box-sizing: border-box;
        .logopic{
            position: relative;
            left: 50%;
            transform: translate(-50%,-50%);
            width: 100px;
            height: 100px;
            border-radius: 50%;
            background-color: #fff;
            padding: 2px;
            box-shadow: 0 0 10px #ddd;
            img{
                width: 100%;
                height: 100%;
                border-radius: 50%;
            }
            
        }
    
    }
</style>